//
//  TNST_FindFriendDemoVC.m
//  CollTest
//
//  Created by codew on 1/2/22.
//

#import "TNST_FindFriendDemoVC.h"
#import "MJExtension.h"
#import "TNST_FFUIPointModel.h"
@interface TNST_FindFriendDemoVC ()

@property (nonatomic, strong) UIBezierPath *path1;
@property (nonatomic, strong) UIBezierPath *path2;
@property (nonatomic, strong) UIBezierPath *path3;

@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;

@end

@implementation TNST_FindFriendDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self data1];
    [self data2];
    [self data3];
    
    self.imageView1.hidden = YES;
    self.imageView2.hidden = YES;
    self.imageView3.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    [self cc1];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self cc2];
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self cc3];
    });
}

- (void)data1
{
    NSArray * arr = [self readLocalFileWithName:@"anm1"];
    
    
    NSArray <TNST_FFUIPointModel *>* arrItem = [TNST_FFUIPointModel mj_objectArrayWithKeyValuesArray:arr];
    
    NSLog(@"%@", arr);
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    _path1 = path;
    
    TNST_FFUIPointModel * pointItem = arrItem[0];
    CGPoint pp1 = CGPointMake(pointItem.x, pointItem.y);
    // 设置起点
    [path moveToPoint:pp1];
    
    
    
//    NSMutableArray * arrP = [NSMutableArray arrayWithArray:arrItem];
    for (TNST_FFUIPointModel * itemPPP in arrItem) {
        CGPoint pp2 = CGPointMake(itemPPP.x, itemPPP.y);
        [_path1 addLineToPoint:pp2];
    }
}

- (void)data2
{
    NSArray * arr = [self readLocalFileWithName:@"anm1"];
    
    
    NSArray <TNST_FFUIPointModel *>* arrItem = [TNST_FFUIPointModel mj_objectArrayWithKeyValuesArray:arr];
    
    NSLog(@"%@", arr);
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    _path2 = path;
    
    TNST_FFUIPointModel * pointItem = arrItem[0];
    CGPoint pp1 = CGPointMake(pointItem.x, pointItem.y);
    // 设置起点
    [path moveToPoint:pp1];
    

    for (int i = 0; i < arrItem.count * 0.7; i++) {
        
        TNST_FFUIPointModel * itemPPP = arrItem[i];
        CGPoint pp2 = CGPointMake(itemPPP.x, itemPPP.y);
        [_path2 addLineToPoint:pp2];
    }
    
    
}

- (void)data3
{
    NSArray * arr = [self readLocalFileWithName:@"anm3"];
    
    
    NSArray <TNST_FFUIPointModel *>* arrItem = [TNST_FFUIPointModel mj_objectArrayWithKeyValuesArray:arr];
    
    NSLog(@"%@", arr);
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    _path3 = path;
    
    TNST_FFUIPointModel * pointItem = arrItem[0];
    CGPoint pp1 = CGPointMake(pointItem.x, pointItem.y);
    // 设置起点
    [path moveToPoint:pp1];
    
    
    
//    NSMutableArray * arrP = [NSMutableArray arrayWithArray:arrItem];
    for (TNST_FFUIPointModel * itemPPP in arrItem) {
        CGPoint pp2 = CGPointMake(itemPPP.x, itemPPP.y);
        [_path3 addLineToPoint:pp2];
    }
}

- (void)cc1
{
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    anim.path = _path1.CGPath;
    
    // 同时缩放，平移，旋转
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *scale = [CABasicAnimation animation];
    scale.keyPath = @"transform.scale";
    scale.fromValue = @1.1;
    scale.toValue = @0.7;

    
    group.animations = @[scale,anim];
    group.duration = 3;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.repeatCount = 1;
    
    self.imageView1.hidden = NO;
    [self.imageView1.layer addAnimation:group forKey:nil];
}
- (void)cc2
{
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    anim.path = _path2.CGPath;
    
    // 同时缩放，平移，旋转
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *scale = [CABasicAnimation animation];
    scale.keyPath = @"transform.scale";
    scale.fromValue = @1.1;
    scale.toValue = @0.9;
    
    group.animations = @[scale,anim];
    group.duration = 2;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.repeatCount = 1;
    self.imageView2.hidden = NO;
    [self.imageView2.layer addAnimation:group forKey:nil];
}
- (void)cc3
{
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    anim.path = _path3.CGPath;
    
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *scale = [CABasicAnimation animation];
    scale.keyPath = @"transform.scale";
    scale.fromValue = @1;
    scale.toValue = @0.86;
                
    group.animations = @[scale,anim];
    group.duration = 1;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.repeatCount = 1;
    
    self.imageView3.hidden = NO;
    [self.imageView3.layer addAnimation:group forKey:nil];
}
// 读取本地JSON文件
- (NSArray *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    
    NSString * str  =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSLog(@"%@", str);
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
